<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="carTypeFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="carTypeFormData.typeName" placeholder="请输入类型"></el-input>
                </el-form-item>
<!--                                <el-form-item >-->
<!--                                    <el-input v-model="userFormData.loginName" placeholder="请输入昵称"></el-input>-->
<!--                                </el-form-item>-->
<!--                                <el-form-item >-->
<!--                                    <el-input v-model="userFormData.email" placeholder="请输入邮箱"></el-input>-->
<!--                                </el-form-item>-->
<!--                                <el-form-item >-->
<!--                                    <el-input v-model="userFormData.phone" placeholder="请输入电话"></el-input>-->
<!--                                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="typeName"
                            label="类型"
                            width="180">
                    </el-table-column>

                    <el-table-column
                            prop="startPrice"
                            label="起步租价（/天）"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="子路由"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="时间"
                            width="180">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.id)"></el-button>
<!--                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>-->
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>

        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
        >
            <!--            model:表单数据-->
            <el-form  label-width="100px" :model="carTypeForm" >
                <el-form-item label="id" style="display: none">
                    <el-input v-model="carTypeForm.id" ></el-input>
                </el-form-item>
                <el-form-item label="类型" >
                    <el-input v-model="carTypeForm.typeName"></el-input>
                </el-form-item>
                <el-form-item label="起步租价(/天)" >
                    <el-input v-model="carTypeForm.startPrice"></el-input>
                </el-form-item>
                <el-form-item label="子路由" >
                    <el-input v-model="carTypeForm.url"></el-input>
                </el-form-item>
                <el-form-item label="时间" >
                    <el-form-item >
                        <el-date-picker
                                v-model="carTypeForm.addtime"
                                type="date"
                                placeholder="日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>


        <el-upload
                class="avatar-uploader"
                action="http://localhost:1000/rents/comment/upload"
                :show-file-list="false"
                :headers="{'token':token}"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

    </div>

</template>

<script>
    export default {
        name: "CarType",
        data(){
            return {
                editDialogVisible:false,
                //条件搜索对象
                carTypeFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                carTypeForm:{},
                imageUrl: '',
                token: "",


            }
        },
        created() {
           this.selectall();
           this.token=sessionStorage.getItem("token");
        },
        methods:{
            handleAvatarSuccess(res, file) {
                this.imageUrl = res.result.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

            formSubmit(){
                var that=this;
                if(this.carTypeForm.id!=null){
                    this.$http.put("rents/type/update",this.carTypeForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.carTypeForm={};
                        that.selectall();
                    })
                }else {
                    this.$http.post("rents/type/insert", this.carTypeForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.selectall();
                    })
                }

            },
            selectall(){
                var that=this;
              this.$http.post(`rents/type/pageSelect/${this.currentPage}/${this.pageSize}`,this.carTypeFormData).then(function (returnResult) {
                          that.total=returnResult.data.result.total;
                          that.tableData=returnResult.data.result.records;
                          console.log(that.tableData)

              })
            },
             chaxun(){
                 this.selectall();
             },
            insert(){
                this.editDialogVisible=true;
                this.carTypeForm={};
            },
            // deleteById(id){ 有外键不能删除
            //     var that=this;
            //     this.$http.delete("rents/type?id="+id).then(function (returnResult) {
            //
            //     })
            // },
            selectById(id){
                var that=this;
                this.$http.get(`rents/type/selectById/${id}`).then(function (returnResult) {
                    that.editDialogVisible=true;
                       that.carTypeForm=returnResult.data.result.huixian;
                       console.log(returnResult)
                })
            },
            handleSizeChange(v){
                this.pageSize=v;
                this.selectall();
            },
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectall();
            }

        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>